﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ufolder
{
    public partial class AddUpdateForm : Form
    {
        private Item displayedItem = null;
        private System.Resources.ResourceManager res = new System.Resources.ResourceManager("ufolder.TextDisplay", System.Reflection.Assembly.GetExecutingAssembly());

        public string Titre
        {
            get
            {

                return tBTitre.Text;
            }
            set
            {
                this.tBTitre.Text = value;
            }
        }

        public string Texte
        {
            get
            {
                return this.tBTexte.Text != "" ? this.tBTexte.Text : null;
            }
            set
            {
                this.tBTexte.Text = value;
            }
        }

        public string Type
        {
            get
            {
                if(this.tBURI.Text == "")
                {
                    return null;
                }
                else
                {
                    return Item.GetTypeFromURI(this.tBURI.Text);
                }
            }
            set
            {
                this.tBTexte.Text = value;
            }
        }

        public string URI
        {
            get
            {
                return this.tBURI.Text != "" ? this.tBURI.Text : null;
            }
            set
            {
                this.tBURI.Text = value;
            }
        }

        public Item DisplayedItem
        {
            get
            {
               return this.displayedItem;
            }
        }

        public void DisplayItem(Item item)
        {
            this.displayedItem = item;
            this.Texte = item.Texte;
            this.Titre = item.Titre;
            this.URI = item.URI;
            this.SetTags(item.Tags);
        }

        public string[] Tags
        {
            get
            {
                if (this.tBTags.Text != "")
                {
                    string[] s = this.tBTags.Text.Trim().Split(' ');
                    int nbrTag = s.Length;
                    for (int i = 0; i < nbrTag; i++)
                    {
                        //s[i] = Label.Filter(s[i]);
                        s[i] = s[i].Trim(new char[] {' '});
                        
                    }
                    return s;
                }
                else
                    return null;
            }
            set
            {
                foreach (string s in value)
                {
                    this.tBTags.Text = s + " ";
                }
            }
        }

        public void SetTags(Label[] ls)
        {
            string t = "";
            foreach (Label l in ls)
                t+=l.Tag+" ";
            this.tBTags.Text = t.TrimEnd(' ');
        }

        public void ShowDel()
        {
            this.bDel.Visible = true;
        }
        
        public AddUpdateForm()
        {
            InitializeComponent();
        }

        private void lTitre_Click(object sender, EventArgs e)
        {

        }

        private void bBrowse_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog.ShowDialog() == DialogResult.OK)
            {
                this.tBURI.Text = openFileDialog.FileName;
            }
        }

        private void bOk_Click(object sender, EventArgs e)
        {
            this.lStatus.Text="";
            bool textV = this.tBTitre.Text != "";

            string[] s = this.tBTags.Text.Split(' ');
            int nbrTag = s.Length;
            bool tagsValid = true;
            for (int i = 0; i < nbrTag && tagsValid; i++)
            {
                tagsValid = Label.CheckValidTag(s[i].Trim(new char[] { ' ' }));
            }

            if (!textV) this.lStatus.Text += res.GetString("badTitle");
            if (!tagsValid) this.lStatus.Text += res.GetString("badTag");

            if (textV && tagsValid)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void bCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void bDel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Abort;
            this.Close();
        }

        private void bGo_Click(object sender, EventArgs e)
        {
            if (this.tBURI.Text != "") System.Diagnostics.Process.Start(this.tBURI.Text);
        }
    }
}
